lunotrades.balcaodeloja = {}
lunotrades.balcaodeloja.loja_atual = {}
lunotrades.balcaodeloja.formspec = {
	customer = function(pos)
		local list_name = "nodemeta:"..pos.x..','..pos.y..','..pos.z
		local formspec = "size[8,10]"..
		"label[2.5,0;MAQUINA DE LOJA]"..
		
		"label[0,0.5;O Cliente Oferece:]"..
		"list[current_player;customer_gives;0,1;3,2;]"..
		"label[0,3;O Cliente Recebe:]"..
		"list[current_player;customer_gets;0,3.5;3,2;]"..
		
		"button[3,1.5;2,1;exchange;ACEITAR]"..
		
		"label[5,0.5;A Maquina Precisa:]"..
		"list["..list_name..";owner_wants;5,1;3,2;]"..
		"label[5,3;A Maquina Oferece:]"..
		"list["..list_name..";owner_gives;5,3.5;3,2;]"..

		"label[0,5.5;Inventario atual do cliente:]"..
		"list[current_player;main;0,6.0;8,4;]"
		
		return formspec
	end,
	owner = function(pos)
		local list_name = "nodemeta:"..pos.x..','..pos.y..','..pos.z
		local formspec = "size[8,10]"..
		"label[0,0;Itens Recebido (Seu Lucro):]"..
		"list["..list_name..";customers_gave;0,0.5;3,2;]"..
		"label[0,2.5;Estoque a Oferetar:]"..
		"list["..list_name..";stock;0,3;3,2;]"..
		"label[5,0;Voce Precisa:]"..
		"list["..list_name..";owner_wants;5,0.5;3,2;]"..
		"label[5,2.5;Voce Oferece:]"..
		"list["..list_name..";owner_gives;5,3;3,2;]"..
		--"label[0,5;Proprietario: Pressione (E) + Botao(RMB) no Mouse para a interface com o cliente]"..
		--"label[0,5;Vendedor: Evite o estoque baixo e guardar lucros no balcao.]"..
		"label[2,5;(CTRL + Mouse = Interface do Cliente)]"..
		"label[0,5.5;Inventario atual do vendedor:]"..
		"list[current_player;main;0,6;8,4;]"
		return formspec
	end,
}

lunotrades.balcaodeloja.getPrivilegio = function(listname,playername,meta)
	--[[if listname == "pl1" then
		if playername ~= meta:get_string("pl1") then
			return false
		elseif meta:get_int("pl1step") ~= 1 then
			return false
		end
	end
	if listname == "pl2" then
		if playername ~= meta:get_string("pl2") then
			return false
		elseif meta:get_int("pl2step") ~= 1 then
			return false
		end
	end]]
	return true
end


lunotrades.balcaodeloja.getInventario = function(inv,list,playername)
	player = minetest.env:get_player_by_name(playername)
	if player then
		for k,v in ipairs(inv:get_list(list)) do
			player:get_inventory():add_item("main",v)
			inv:remove_item(list,v)
		end
	end
end

lunotrades.balcaodeloja.cancel = function(meta)
	--[[lunotrades.balcaodeloja.getInventario(meta:get_inventory(),"pl1",meta:get_string("pl1"))
	lunotrades.balcaodeloja.getInventario(meta:get_inventory(),"pl2",meta:get_string("pl2"))
	meta:set_string("pl1","")
	meta:set_string("pl2","")
	meta:set_int("pl1step",0)
	meta:set_int("pl2step",0)]]
end

lunotrades.balcaodeloja.exchange = function(meta)
	--[[lunotrades.balcaodeloja.getInventario(meta:get_inventory(),"pl1",meta:get_string("pl2"))
	lunotrades.balcaodeloja.getInventario(meta:get_inventory(),"pl2",meta:get_string("pl1"))
	meta:set_string("pl1","")
	meta:set_string("pl2","")
	meta:set_int("pl1step",0)
	meta:set_int("pl2step",0)]]
end

local box_format = {
	type = "fixed",
	fixed = { {-.5,-.5,-.3125,    .5,.5+1/3,.3125}	}
}

minetest.register_node("lunotrades:balcaodeloja", {
	description = "Maquina de Escambo",
	--tiles = {"balcao_topo.png", "balcao2_baixo.png", "balcao2_lado.png"},
	
	drawtype = "nodebox",
	paramtype = "light", --Nao sei pq, mas o blco nao aceita a luz se nao tiver esta propriedade
	paramtype2 = "facedir",
	sunlight_propagates = true,
	light_source = LIGHT_MAX,
	node_box = box_format,
	selection_box = box_format,
	tiles = {
		"dispensador_cima.png", 
		"dispensador_baixo.png", 
		"dispensador_esquerda.png", 
		"dispensador_direita.png", 
		"dispensador_traz.png", 
		"dispensador_frente.png"--.."^[transformfx"
	},
	groups = {snappy=2,choppy=2,oddly_breakable_by_hand=2},
	sounds = default.node_sound_wood_defaults(),
	after_place_node = function(pos, placer, itemstack)
		local owner = placer:get_player_name()
		local meta = minetest.env:get_meta(pos)
		meta:set_string("infotext", "Maquina de Escambo de '"..owner.."'")
		meta:set_string("owner",owner)
		--[[meta:set_string("pl1","")
		meta:set_string("pl2","")]]
		local inv = meta:get_inventory()
		inv:set_size("customers_gave", 3*2)
		inv:set_size("stock", 3*2)
		inv:set_size("owner_wants", 3*2)
		inv:set_size("owner_gives", 3*2)
	end,
	on_rightclick = function(pos, node, clicker, itemstack)
		--print("lunotrades:balcaodeloja.on_rightclick aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
		clicker:get_inventory():set_size("customer_gives", 3*2)
		clicker:get_inventory():set_size("customer_gets", 3*2)
		lunotrades.balcaodeloja.loja_atual[clicker:get_player_name()] = pos
		local meta = minetest.env:get_meta(pos)
		if (clicker:get_player_name() == meta:get_string("owner") or minetest.check_player_privs(clicker:get_player_name(), {landrush=true}) ) and not clicker:get_player_control().aux1 then
			minetest.show_formspec(clicker:get_player_name(),"lunotrades.balcaodeloja_formspec",lunotrades.balcaodeloja.formspec.owner(pos))
		else
			minetest.show_formspec(clicker:get_player_name(),"lunotrades.balcaodeloja_formspec",lunotrades.balcaodeloja.formspec.customer(pos))
		end
		--return itemstack
	end,
	allow_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
		local playername = player:get_player_name()
		local meta = minetest.env:get_meta(pos)
		if player:get_player_name() ~= meta:get_string("owner") and not minetest.check_player_privs(playername, {landrush=true}) then return 0 end
		return count
	end,
	allow_metadata_inventory_put = function(pos, listname, index, stack, player)
		local playername = player:get_player_name()
		local meta = minetest.env:get_meta(pos)
		if player:get_player_name() ~= meta:get_string("owner") and not minetest.check_player_privs(playername, {landrush=true}) then return 0 end
		return stack:get_count()
	end,
	allow_metadata_inventory_take = function(pos, listname, index, stack, player)
		local playername = player:get_player_name()
		local meta = minetest.env:get_meta(pos)
		if player:get_player_name() ~= meta:get_string("owner") and not minetest.check_player_privs(playername, {landrush=true}) then return 0 end
		return stack:get_count()
	end,
	can_dig = function(pos, player)
		local meta = minetest.env:get_meta(pos)
		local inv = meta:get_inventory()
		return inv:is_empty("stock") and inv:is_empty("customers_gave") and inv:is_empty("owner_wants") and inv:is_empty("owner_gives")
	end
})

minetest.register_on_player_receive_fields(function(sender, formname, fields)
	if formname == "lunotrades.balcaodeloja_formspec" then
		local name = sender:get_player_name()
		local pos = lunotrades.balcaodeloja.loja_atual[name]
		local meta = minetest.env:get_meta(pos)
		local owner = meta:get_string("owner")
		--minetest.chat_send_player(name,"owner('"..owner.."') == name('"..name.."')")
		if owner == name or (minetest.check_player_privs(name, {landrush=true}) and sender:get_player_control().aux1)then
			minetest.chat_send_player(name,"Voce nao pode trocar na sua propria maquina!")
			minetest.sound_play("sfx_alert", {object=sender, max_hear_distance=5.0,})
			return
		else
			local minv = meta:get_inventory()
			local pinv = sender:get_inventory()
			local invlist_tostring = function(invlist)
				local out = {}
				for i, item in pairs(invlist) do
					out[i] = item:to_string()
				end
				return out
			end
			local wants = minv:get_list("owner_wants")
			local gives = minv:get_list("owner_gives")
			if wants == nil or gives == nil then return end -- do not crash the server
			-- Check if we can exchange
			local can_exchange = true
			local owners_fault = false
			for i, item in pairs(wants) do
				if not pinv:contains_item("customer_gives",item) then
					can_exchange = false
				end
			end
			for i, item in pairs(gives) do
				if not minv:contains_item("stock",item) then
					can_exchange = false
					owners_fault = true
				end
			end
			if can_exchange then
				for i, item in pairs(wants) do
					pinv:remove_item("customer_gives",item)
					minv:add_item("customers_gave",item)
				end
				for i, item in pairs(gives) do
					minv:remove_item("stock",item)
					pinv:add_item("customer_gets",item)
				end
				minetest.chat_send_player(name,"Escambo feito!")
				minetest.sound_play("sfx_cash_register", {object=sender, max_hear_distance=5.0,})
			else
				if owners_fault then
					minetest.chat_send_player(name,"O estoque de '"..owner.."' acabou. Contacte-o!")
				else
					minetest.chat_send_player(name,"O escambo nao pode ser feito. Verifique se voce ofereceu o que a maquina pede!")
				end
				minetest.sound_play("sfx_alert", {object=sender, max_hear_distance=5.0,})
			end
		end
	end
end)

minetest.register_craft({
	output = 'lunotrades:balcaodeloja',
	recipe = {
		{"dye:dark_green"	,"default:steel_ingot"	,"default:steel_ingot"},
		{"default:glass"	,""							,"default:steel_ingot"},
		{"dye:dark_green"	,"default:steel_ingot"	,"default:mese"},
		--COMENTaRIO: dye:dark_green = dye:blue[B2] + dye:yellow[C2]
	}
})


minetest.register_alias("maquinadeloja"		,"lunotrades:balcaodeloja")
minetest.register_alias("maquinadeescambo"	,"lunotrades:balcaodeloja")
minetest.register_alias("dispensadora"			,"lunotrades:balcaodeloja")
minetest.register_alias("balcaodeloja"			,"lunotrades:balcaodeloja")
minetest.register_alias("caixadeloja"			,"lunotrades:balcaodeloja")
minetest.register_alias("loja"					,"lunotrades:balcaodeloja")
